package com.atech.ebayadmin.category.action;

import java.io.Writer;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.atech.ebayadmin.category.model.Category;
import com.atech.ebayadmin.category.service.ICategoryService;
import com.atech.ebayadmin.common.struts.BaseAction;

public class CategoryAction  extends BaseAction{

	private static final long serialVersionUID = 906121125358766647L;
	private static final Log log = LogFactory.getLog(CategoryAction.class);
	protected List pathToTop = new ArrayList();
	protected Integer id = 0;
	protected List children = new ArrayList();
	private ICategoryService categoryService;
	
	/**
	 * @return the categoryService
	 */
	public ICategoryService getCategoryService() {
		return categoryService;
	}

	/**
	 * @param categoryService the categoryService to set
	 */
	public void setCategoryService(ICategoryService categoryService) {
		this.categoryService = categoryService;
	}

	/**
	 * @return the children
	 */
	public List getChildren() {
		return children;
	}

	/**
	 * @param children the children to set
	 */
	public void setChildren(List children) {
		this.children = children;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the pathToTop
	 */
	public List getPathToTop() {
		return pathToTop;
	}

	/**
	 * @param pathToTop the pathToTop to set
	 */
	public void setPathToTop(List pathToTop) {
		this.pathToTop = pathToTop;
	}

	public String readById() throws Exception {
		try{
			if(this.getId()==null)
			{
				this.setId(new Integer(0));
			}
			
			this.setChildren(categoryService.listCategoryByParentId(this.getId().toString()));
			List templist= categoryService.ascendToTop(this.getId().toString());
			if (templist.equals(null)) // added by proby to indicate an error, force redirect to other page.
				return INPUT;
			this.setPathToTop(templist);
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
    /**
     * 用无刷新技术，根据父类ID查询子分类
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String getAllSortAjax() throws Exception{
    	HttpServletResponse response = super.getResponse();
    	HttpServletRequest request = super.getRequest();
    	response.setCharacterEncoding("UTF-8");
        Writer out = response.getWriter();
        String parentId = request.getParameter("parentId");
        String selectName = request.getParameter("selectName");
        String info="fail";
		try {
			List<Category> sortList = categoryService.listCategoryByParentId(parentId);
			if(sortList != null && sortList.size() >0){
		        StringBuffer select = new StringBuffer("<select name='");
		        select.append(selectName).append("'");
		        select.append(" id=").append(selectName).append("'");
		        select.append(" onchange=\"getSortSelectAjax('"+selectName+"');\"");
		        select.append("style='width:200px' >");
		        select.append("<option value='all'>"+ "please choose" +"</option>");
				
					for(int i=0;i<sortList.size();i++){
						Category category = sortList.get(i);
						select.append("<option value='"+category.getId()+"'>"+category.getName()+"</option>");
					}
				
				select.append("</select>");
				info = select.toString();
				log.info("用ajax查询子分类:"+info);
			}else{
				info = "no result";
			}
		} catch (Exception e) {
			log.error("用ajax查询子分类失败!", e);
		} 
    	out.write(info);
    	out.flush();
    	out.close();
        return null;
    }
}
